﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using HCCommon;
using HCMessage.Model;

namespace HCData
{
    public class EnvironmentCategory
    {

        public List<EnvCategory> GetEnvironmentGategoryList()
        {
            var result = new List<EnvCategory>();
            using (var con = new SqlConnection(DBConnection.GetConnection().ConnectionString))
            {
                con.Open();
                var cmd = con.CreateCommand();
                cmd.CommandText = "SELECT * FROM DBO.HC_ENV";
                cmd.CommandType = CommandType.Text;
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    result.Add(new EnvCategory
                    {
                        EnvCode = DBNull.Value == reader["ENV_CD"] ? "" : reader["ENV_CD"].ToString(),
                        EnvName = DBNull.Value == reader["ENV_NM"] ? "" : reader["ENV_NM"].ToString()
                    });
                }
            }
            return result;
        }

        public int UpdateItemName(string ItemName)
        {
            var result = 0;
            using (var con = new SqlConnection(DBConnection.GetConnection().ConnectionString))
            {
                con.Open();
                var cmd = con.CreateCommand();
                cmd.CommandText = "DBO.HC_ITEM_UPDATE";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ITEM_NM", Value = ItemName, Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@USR_CRTN_ID", Value = "zhenwu", Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ITEM_ID", Value = 0, Direction = ParameterDirection.Output });
                cmd.ExecuteNonQuery();
                var o1 = cmd.Parameters["@ITEM_ID"] as SqlParameter;
                result = o1.Value == DBNull.Value ? 0 : Convert.ToInt32(o1.Value);
            }
            return result;
        }

        public int UpdateItemDetailName(EnvEntity detail)
        {
            var result = 0;
            using (var con = new SqlConnection(DBConnection.GetConnection().ConnectionString))
            {
                con.Open();
                var cmd = con.CreateCommand();
                cmd.CommandText = "DBO.HC_ITEM_DETAIL_UPDATE";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ITEM_ID", Value = detail.ItemId, Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ITEM_ADDRESS", Value = detail.ItemAddress, Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ENV_CD", Value = detail.ItemEnvironment, Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@USR_CRTN_ID", Value = "zhenwu", Direction = ParameterDirection.Input });
                cmd.Parameters.Add(new SqlParameter { ParameterName = "@ITEM_DETAIL_ID", Value = 0, Direction = ParameterDirection.Output });
                cmd.ExecuteNonQuery();
                var o1 = cmd.Parameters["@ITEM_DETAIL_ID"] as SqlParameter;
                result = o1.Value == DBNull.Value ? 0 : Convert.ToInt32(o1.Value);
            }
            return result;
        }

        public List<ItemReportDetail> GetItemReportDetail()
        {
            var result = new List<ItemReportDetail>();
            using (var con = new SqlConnection(DBConnection.GetConnection().ConnectionString))
            {
                con.Open();
                var cmd = con.CreateCommand();
                cmd.CommandText = @"SELECT ITEM_DETAIL_ID,ITEM_NM,ITEM_ADDRESS,ENV_CD
                                   FROM DBO.HC_ITEM_DETAIL DE
                                   LEFT JOIN DBO.HC_ITEM ITEM
                                   ON ITEM.ITEM_ID=DE.ITEM_ID";
                cmd.CommandType = CommandType.Text;
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    result.Add(new ItemReportDetail
                    {
                        ItemDetailId = DBNull.Value == reader["ITEM_DETAIL_ID"] ? 0 :Convert.ToInt32(reader["ITEM_DETAIL_ID"]),
                        Name = DBNull.Value == reader["ITEM_NM"] ? "" : reader["ITEM_NM"].ToString(),
                        Address = DBNull.Value == reader["ITEM_ADDRESS"] ? "" : reader["ITEM_ADDRESS"].ToString(),
                        EnvCd = DBNull.Value == reader["ENV_CD"] ? "" : reader["ENV_CD"].ToString()
                    });
                }
            }
            return result;
        }

    }
}
